<?php
session_start();
?>

<html>
<head>
<style type="text/css" media="only screen and (max-device-width: 480px)">
table { font-family:Arial, Helvetica, sans-serif;}
a {font-family:Arial, Helvetica, sans-serif; font-size:80px; font-weight:bold;}
font {font-size:55px;}
.font {font-size:55px;}
input {width:90; height:100}
</style>
</head>
<body>

<?php
$pid = $_SESSION['pid'];

/**
*
* PatientData.php is the page on the webapp that displays the patient data 
* to the users. As of right now, it is funtional for the HARDCODED CASE of
* there being one patient in patient medical data. What will need to happen is that
* the medical data gathered from the db should be based on the patient in the 
* emergency. Based on current logic, this can only happen once, though it could
* be expanded to handle more than one using a while loop around the echo that prints
* out all the html
*
*
* Authors: Ahmad Sh, Joseph Fillmore
*/
$_SESSION['dbhost'] = 'engr-db.engr.oregonstate.edu:3307';
$_SESSION['dbname'] = 'funkecd';
$_SESSION['dbuser'] = 'funkecd';
$_SESSION['dbpass'] = 'ktrDtj1b';


$mysql_handle = mysql_connect($_SESSION['dbhost'], $_SESSION['dbuser'], $_SESSION['dbpass'])
    or die("Error connecting to database server");

mysql_select_db($_SESSION['dbname'], $mysql_handle)
    or die("Error selecting database: $dbname");


$query = "SELECT * FROM `Patient_Medical_Data` WHERE `pid` = $pid";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

if($row == null && $pid == null)
{
	$query = "SELECT * FROM `Patient_Medical_Data` ORDER BY `pid` DESC";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$pid = $row['pid'];
	//echo " ROWPID: " .$row['pid']. "";
}

$query = "SELECT `patient_first_name` , `patient_last_name` FROM `Patient_List` WHERE `pid` =$pid";
$result = mysql_query($query);
$altRow = mysql_fetch_array($result);

mysql_close($mysql_handle);
//list of variables to check and set based on session
if ($_SESSION['gravida'] != null)
{
	$row['gravida'] = $_SESSION['gravida'];
	$_SESSION['gravida'] = null;
}
if ($_SESSION['para'] != null)
{
	$row['para'] = $_SESSION['para'];
	$_SESSION['para'] = null;
}
if ($_SESSION['gestation_age'] != null)
{
	$row['grestation_age'] = $_SESSION['gestation_age'];
	$_SESSION['gestation_age'] = null;
}
if ($_SESSION['reason_for_cs'] != null)
{
	$row['reason_for_cs'] = $_SESSION['reason_for_cs'];
	$_SESSION['reason_for_cs'] = null;
}
if ($_SESSION['current_medications'] != null)
{
	$row['current_medications'] = $_SESSION['current_medications'];
	$_SESSION['current_medications'] = null;
}
if ($_SESSION['allergies'] != null)
{
	$row['allergies'] = $_SESSION['allergies'];
	$_SESSION['allergies'] = null;
}
if ($_SESSION['health_during_pregnacny'] != null)
{
	$row['health_during_pregnancy'] = $_SESSION['health_during_pregnacny'];
	$_SESSION['health_during_pregnacny'] = null;
}
if ($_SESSION['past_major_illnesses'] != null)
{
	$row['past_major_illnesses'] = $_SESSION['past_major_illnesses'];
	$_SESSION['past_major_illnesses'] = null;
}
if ($_SESSION['membrane_rupture_color'] != null)
{
	$row['membrane_rupture_color'] = $_SESSION['membrane_rupture_color'];
	$_SESSION['membrane_rupture_color'] = null;
}
if($_SESSION['membrane_rupture_amount'] != null)
{
	$row['membrane_rupture_amount'] = $_SESSION['membrane_rupture_amount'];
	$_SESSION['membrane_rupture_amount'] = null;
}
if ($_SESSION['cervix_position'] != null)
{
	$row['cervix_position'] = $_SESSION['cervix_position'];
	$_SESSION['cervix_position'] = null;
}
if ($_SESSION['uterus_size'] != null)
{
	$row['uterus_size'] = $_SESSION['uterus_size'];
	$_SESSION['uterus_size'] = null;
}
if ($_SESSION['uterus_lie'] != null)
{
	$row['uterus_lie'] = $_SESSION['uterus_lie'];
	$_SESSION['uterus_lie'] = null;
}
if ($_SESSION['presentation'] != null)
{
	$row['presentation'] = $_SESSION['presentation'];
	$_SESSION['presentation'] = null;
}
if ($_SESSION['dilation'] != null)
{
	$row['dilation'] = $_SESSION['dilation'];
	$_SESSION['dilation'] = null;
}
if ($_SESSION['contraction_interval'] != null)
{
	$row['contraction_interval'] = $_SESSION['contraction_interval'];
	$_SESSION['contraction_interval'] = null;
}
if ($_SESSION['contraction_length'] != null)
{
	$row['contraction_length'] = $_SESSION['contraction_length'] ;
	$_SESSION['contraction_length']  = null;
}
if ($_SESSION['contraction_intensity'] != null)
{
	$row['contraction_intensity'] = $_SESSION['contraction_intensity'];
	$_SESSION['contraction_intensity'] = null;
}

if ($altRow['patient_first_name'] == null)
{
	$altRow['patient_first_name'] = "no first name entered";
}
if ($altRow['patient_last_name'] == null)
{
	$altRow['patient_last_name'] = "no last name entered";
}

echo '
<td valign="top"><font size = "6" face= Arial, Helvetica, sans-serif>Patient Medical Information</font><br>';
//echo '<a class="hide" href="http://web.engr.orst.edu/~sharifpa/cs462repos/links.html"> Menu </a><br><br>';
echo'
<font face= Arial, Helvetica, sans-serif>
<table border="1" width="75%" bordercolor=blue> ';
if($_SESSION['inserted'] == true)
{
	echo "<b>Patient Medical Data Successfully Entered!</b><br>";
	$_SESSION['inserted'] = false;
}
else if ($_SESSION['error'] == true)
{
	echo" <font color = \"red\" >There was at least one error with your request</font>";
	$_SESSION['error'] = false;
}
echo'
<tr><td>
<form method="post" action="DBInput_PatientData.php">
<br>

<table class = "font">
<tr><td> Patient ID: <td>' . $pid . ' </tr> 
<tr><td>Patient First Name:  <td > ' . $altRow['patient_first_name'] . '
<tr><td>Patient Last Name:  <td> ' . $altRow['patient_last_name'] . '
<tr><td>Gravida:  <td>
<input type="text" name="gravida" value="' . $row['gravida']  . '" cols="5" rows="1"/>
';
if ($_SESSION['gravErr'] == true) {echo "<font color = \"red\" >Must enter a Number for Gravida </font>"; $_SESSION['gravErr'] = false;} 
echo'
<tr><td>Para: <td>
<input type="text" name="para" value="' . $row['para'] . '" cols="5" rows="1"/>
';
if ($_SESSION['Paraerr'] == true) {echo "<font color = \"red\" >Must enter a Number for Para</font>"; $_SESSION['Paraerr'] = false;} 
echo'
<tr><td>Gestation Age: <td>
<input type="text" name="gestation_age" value="' .$row['grestation_age'] . '" cols="5"rows = "1"/> Weeks
';
if ($_SESSION['GAgeerr'] == true) {echo "<font color = \"red\" >Must enter a Number for Gestation Age</font>"; $_SESSION['GAgeerr'] = false;} 
echo'
</table>


<br>
</td>
<tr><td>
<br>

<table class = "font">
<tr><td>Reasons for CS: <td>
<input type="text" value = "' . $row['reason_for_cs'] . '" name="reason_for_cs" cols="20"rows = "1"/>
';
if ($_SESSION['RFCSerr'] == true) {echo "<font color = \"red\" >Reason for CS must be text</font>"; $_SESSION['RFCSerr'] = false;} 
echo'

</table>

<br>
</td>
<tr><td>
<br>


<table class="font">

<tr><td>Current Medications: <td>
<input type="text" name="current_medications" value="' . $row['current_medications'] . '"cols="20"rows = "1"/>
';
if ($_SESSION['CurMerr'] == true) {echo '<font color = "red">Current Medications must be text</font>'; $_SESSION['CurMerr'] = false;} 
echo'

<tr><td>Allergies: <td> 
<input type="text" name="allergies" value = "' . $row['allergies'] . '" cols="20"rows = "1"/>
';
if ($_SESSION['AllEerr'] == true) {echo '<font color = "red">Allergies must be text</font>'; $_SESSION['AllEerr'] = false;} 
echo'

<tr><td>Health During Pregnancy: <td> 
<input type="text" name="health_during_pregnancy" value = "' . $row['health_during_pregnancy'] . '" cols="20"rows = "1"/>
';
if ($_SESSION['HDPRerr'] == true) {echo '<font color = "red">Health DUring Pregnancy must be text</font>'; $_SESSION['HDPRerr'] = false;} 
echo'

<tr><td>Past Major Illnessess: <td> 
<input type="text" name="past_major_illnesses" value = "' . $row['past_major_illnesses'] . '" cols="20"rows = "1"/>
';
if ($_SESSION['PMILerr'] == true) {echo '<font color = "red">Past Major Illnesses must be Text</font>'; $_SESSION['PMILerr']= false;} 
echo'

</table>


<br>
</td>
<tr><td>
<br>

<table class="font">
<tr><td>

<tr><td>Membrane Rupture:<td>
<tr><td>Color: <td>  
<input type="text" name="membrane_rupture_color" value = "' . $row['membrane_rupture_color'] . '" cols="5"rows = "1"/>
';
if ($_SESSION['MRCOerr'] == true) {echo '<font color = "red">Membrane Rupture Color must be text</font>'; $_SESSION['MRCOerr'] = false;} 
echo'
<tr><td>Amount: <td> 
<input type="text" name="membrane_rupture_amount" value = "' . $row['membrane_rupture_amount'] . '" cols="5"rows = "1"/> fl.oz.
';
if ($_SESSION['MRAMerr'] == true) {echo '<font color = "red">Membrane Rupture Amount must be a number</font>'; $_SESSION['MRAMerr']  = false;} 
echo'
<tr><td>Time: <td> ' .  $row['membrane_rupture_time'] . ' 
</table>

<br>
</td>
<tr><td>
<br>

<table class="font">
<tr><td>

<tr><td>Cervix Position: <td> 
<input type="text" name="cervix_position" value = "' . $row['cervix_position']  . '" cols="10"rows = "1"/>
';
if ($_SESSION['CPOSerr'] == true) {echo '<font color = "red">Cervix Position must be text</font>'; $_SESSION['CPOSerr'] = false;} 
echo'
<tr><td>Uterus Size: <td> 

<input type="text" name="uterus_size" value = "'. $row['uterus_size'] .'"cols="5"rows = "1"/> inch(s)
';
if ($_SESSION['USIZerr'] == true) {echo '<font color = "red">Uterus Size must be a number</font>'; $_SESSION['USIZerr']= false;} 
echo'
<tr><td>Uterus Lie: <td> 

<input type="text" name="uterus_lie" value = "'. $row['uterus_lie'] .'"cols="15"rows = "1"/>
';
if ($_SESSION['ULIEerr'] == true) {echo '<font color = "red">Uterus Lie must be text</font>'; $_SESSION['ULIEerr'] = false;} 
echo'
<tr><td>Presentation: <td> 

<input type="text" name="presentation" value = "'. $row['presentation'] .'"cols="10"rows = "1"/>
';
if ($_SESSION['PRESerr'] == true) {echo '<font color = "red">Presentation must be text</font>';$_SESSION['PRESerr'] = false;}  
echo'
<tr><td>Dilation: <td> 

<input type="text" name="dilation" value = "'. $row['dilation'] .'"cols="10"rows = "1"/>
';
if ($_SESSION['DILAerr'] == true) {echo '<font color = "red">Dilation must be a number</font>'; $_SESSION['DILAerr']  = false;} 
echo'

</table>

<br>
</td>
<tr><td>
<br>

<table class="font">
<tr><td>

<tr><td>Contraction Interval: <td> 
<input type="text" name="contraction_interval" value = "'. $row['contraction_interval'] .'" cols="5"rows = "1"/> min
';
if ($_SESSION['CINTerr'] == true) {echo '<font color = "red">Contraction Interval must be a number</font>'; $_SESSION['CINTerr']= false;} 
echo'
<tr><td>Contraction Duration: <td> 
<input type="text" name="contraction_length" value = "'.$row['contraction_length'] .'"cols="5"rows = "1"/> sec
';
if ($_SESSION['CLENerr'] == true) {echo '<font color = "red">Contraction Duration must be a number</font>'; $_SESSION['CLENerr']= false;} 
echo'
<tr><td>Contraction Intensity: <td> 
<input type="text" name="contraction_intensity" value = "' .$row['contraction_intensity'] .'"cols="5"rows = "1"/>
';
if ($_SESSION['CINYerr'] == true) {echo '<font color = "red">Contraction Intensity must be a number</font>'; $_SESSION['CINYerr'] = false;} 
echo'
</table>

<br>
</td>

</td></tr></table>
</font>

<input type="submit" value="Submit" />
</form>

</td>
</tr>
</table>
</body>
</html> ';
?>
